package com.yulebaby.callsenter.record.manager;

import android.media.MediaRecorder;
import android.os.Environment;

import com.yulebaby.callsenter.utils.DateUtil;

import java.io.File;

/**
 * Author: Zhangbo
 * Time: 2016/11/9
 */
public class AudioFileFunc {
    //音频输入-麦克风   TODO 声音源的验证   由于走MIC预计有声音过小的问题  预计解决方式 通过系统或者底层监控麦克风的方式
    public final static int AUDIO_INPUT = MediaRecorder.AudioSource.MIC;

    //采用频率
    //44100是目前的标准，但是某些设备仍然支持22050，16000，11025
    public final static int AUDIO_SAMPLE_RATE = 44100;  //44.1KHz,普遍使用的频率
    //录音输出文件
    public static String AUDIO_RAW_FILENAME = "YuLeBaBy.raw";
    public static String AUDIO_WAV_FILENAME = "YuLeBaBy.wav";
    public static String AUDIO_AMR_FILENAME = "YuLeBaBy.amr";


    public static final String FILE_BASE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "YuLeBaBy" + File.separator + "RecordFiles" + File.separator;
    private static String mRecordPath = "";

    /**
     * 判断是否有外部存储设备sdcard
     *
     * @return true | false
     */
    public static boolean isSdcardExit() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
            return true;
        else
            return false;
    }

    /**
     * 检查文件路径是否存在,不存在则建立
     */
    public static void checkFileDirExists() {
        mRecordPath = FILE_BASE_PATH + DateUtil.getCurrentDateString();
        File file = new File(mRecordPath);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    /**
     * 获取麦克风输入的原始音频流文件路径
     *
     * @return
     */
    public static String getRawFilePath() {
        String mAudioRawPath = "";
        if (isSdcardExit()) {
            checkFileDirExists();
            mAudioRawPath = mRecordPath + File.separator + AUDIO_RAW_FILENAME;
        }

        return mAudioRawPath;
    }

    /**
     * 获取编码后的WAV格式音频文件路径
     *
     * @return
     */
    public static String getWavFilePath() {
        String mAudioWavPath = "";
        if (isSdcardExit()) {
            checkFileDirExists();
            mAudioWavPath = mRecordPath + File.separator + AUDIO_WAV_FILENAME;
        }
        return mAudioWavPath;
    }


    /**
     * 获取编码后的AMR格式音频文件路径
     *
     * @return
     */
    public static String getAMRFilePath() {
        String mAudioAMRPath = "";
        if (isSdcardExit()) {
            checkFileDirExists();
            mAudioAMRPath = mRecordPath + File.separator + AUDIO_AMR_FILENAME;
        }
        return mAudioAMRPath;
    }


    /**
     * 获取文件大小
     *
     * @param path,文件的绝对路径
     * @return
     */
    public static long getFileSize(String path) {
        File mFile = new File(path);
        if (!mFile.exists())
            return -1;
        return mFile.length();
    }
}
